
const DURATION = 4000
const TURNNUM = 5

const service = require('../../../service/other')

Page({

    data: {
        isTrue: false, // 是否允许抽奖
        awardsList: [],
        isStarting: false,
        animation: null,
        animationData: '',
        lottery: {},
        awardsIndex: 0,
    },

    /**
     * 初始化
     */
    onLoad: function (options) {
        let animation = wx.createAnimation({
            duration: DURATION,
            timingFunction: 'ease'
        })
        this.setData({ animation: animation })
        this.initData()
    },
    initData() {
        let id = wx.getStorageSync("userInfo").id
        service.getCurrentLottery(id, (res) => {
            const data = res.data
            let isTrue = data.intergrall > data.lotteryTurntable.intergral
            this.setData({
                lottery: res.data,
                isTrue: isTrue
            })
            this.initAwardList(res.data.truntablePrizeList || []);
        })
    },
    /**
     * 初始化抽奖
     */
    initAwardList: function (list) {
        // 绘制转盘
        var awardsList = [];
        var angle = 360 / list.length;
        for (var i = 0; i < list.length; i++) {
            awardsList.push({
                degree: {
                    itemRoate: i * angle,
                    item: `${i * angle}deg`,
                    line: `${(i + 0.5) * angle}deg`,
                    bg_1: `${(i - 0.5) * angle + 90}deg`,
                    bg_2: `${angle - 90}deg`
                },
                id: list[i].id,
                award: list[i].prizeTitle,
                color: this.getRandomColor(i, list.length)
            })
        }
        this.setData({
            awardsList: awardsList
        });
    },

    /**
     * 开始抽奖
     */
    getRndInteger(min, max) {
        return Math.floor(Math.random() * (max - min)) + min;
    },
    getLottery: function () {
        let isTrue = this.data.isTrue

        if (!isTrue) {
            wx.showToast({
                title: '当前积分不足,无法抽奖！',
                icon: 'none'
            })
            return
        }
        let isStarting = this.data.isStarting
        if (isStarting) return;


        /// 获奖序号
        this.getDefaultWard((res) => {
            //清楚掉动画状态
            let orginRoteData = wx.createAnimation({
                duration: 0,
                timingFunction: 'linear'
            })
            orginRoteData.rotate(0).step();
            this.setData({ isStarting: true, animationData: orginRoteData.export() })
            let awardsList = this.data.awardsList
            let randomIndex = 0
            awardsList.forEach((item, index) => {

                if (item.id == res.id) randomIndex = index;
            })
            this.setData({ awardsIndex: randomIndex })
            let itemRoate = awardsList[randomIndex].degree.itemRoate
            this.runDegs = (360 - itemRoate) + 360 * TURNNUM
            console.log(1111, 360 - itemRoate, awardsList[randomIndex])
            /// 开始转动
            this.startRun(randomIndex, this.runDegs, DURATION, res);
        })




    },

    /**
     * 开始转动
     */
    startRun: function (awardIndex, runDegs, duration = DURATION, res) {
        /// 动画
        let animation = wx.createAnimation({
            duration: duration,
            timingFunction: 'ease'
        })
        animation.rotate(runDegs).step();
        this.setData({
            animationData: animation.export()
        })
        /// 中奖提示
        let time = setTimeout(() => {
            this.stopRun(res);
            clearTimeout(time)
        }, duration + 200)
    },


    getDefaultWard(callback) {
        let lottery = this.data.lottery
        let param = {
            id: lottery.lotteryTurntable.id,
            customerId: wx.getStorageSync("userInfo").id
        }
        //网络请求 抽奖？
        service.startLottery(param, (res) => {
            if (res.code = '00000') {
                callback(res.data)
                return
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none'
                })
            }
        })
    },

    /**
     * 结束转动
     */
    stopRun: function (res) {
        if (res['1']) {
            wx.showToast({
                title: res['1'],
                icon: 'none'
            })
        } else if (res['0']) {
            const accountInfo = {
                shoppingList: shoppingList,
                totalMoney: this.data.totalMoney
            }
            wx.redirectTo({
                url: '',
            })
        }
        this.initData()
        this.setData({ isStarting: false })
    },

    /**
     * 获取随机颜色
     */
    getRandomColor: function (i, length) {
        //偶数
        if (length % 2 == 0) {
            if (i % 2 == 0) {
                return "#FF3C3C"
            } else {
                return '#FF9D38'
            }
        } else {
            var func = function (color) {
                return (color += '0123456789abcdefg'[Math.floor(Math.random() * 16)]) && (color.length == 6) ? color : func(color);
            }
            return `#${func('')}`
        }
    }
})


// 旋转抽奖
// this.runDegs = this.runDegs || awardsList[randomIndex].degree.itemRoate;
// this.runDegs = this.runDegs + (360 - this.runDegs % 360) + (360 * runNum - randomIndex * (360 / this.data.awardsList.length));